<template>
  <el-container class="layout-container-demo" style="height: 100%">
    <div style="height: 100%;">
      <el-aside width="200px">
        <el-scrollbar>
          <el-menu :default-active="mainStore.activeIndex" @select="selectHandler">
            <component v-for="item in menu" :key="item.id" :index="item.route"
              :is="item.menu === 1 ? 'el-sub-menu' : 'el-menu-item'">
              <template #title>
                <el-icon>
                  <component :is="item.icon" />
                </el-icon>
                {{ item.name }}
              </template>
              <el-menu-item v-for="t in item.children" :key="t.id" :index="t.route">
                <template #title>
                  <el-icon>
                    <component :is="t.icon" />
                  </el-icon>
                  {{ t.name }}
                </template>
              </el-menu-item>
            </component>
          </el-menu>
        </el-scrollbar>

      </el-aside>
    </div>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <div class="toolbar">
          <el-dropdown trigger="click" @command="commandHandler">
            <div style="display: flex; align-items: center">
              <el-avatar :size="25" :src="userStore.userInfo.avatar" />
              <span style="margin-left: 10px">{{ userStore.userInfo.uname }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <el-scrollbar>
          <RouterView />
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { getMenu } from '@/api/MainApi'
import type { IMenu } from '@/interfaces/IApi'
import router from '@/router'
import { useUserStore } from '@/stores/UserStore'
import { useMainStore } from '@/stores/MainStore'
import { onBeforeMount, ref, type Ref } from 'vue'
const menu: Ref<IMenu[]> = ref([])
const userStore = useUserStore()
const mainStore = useMainStore()
const activeMenu = ref('')
onBeforeMount(async () => {
  menu.value = (await getMenu()).data
  if (!mainStore.activeIndex || mainStore.activeIndex === '/') {
    mainStore.activeIndex = '/weathers'
  }
})
const selectHandler = (index: string) => {
  mainStore.activeIndex = index
}
const commandHandler = (command: string) => {
  if (command === 'logout') {
    userStore.clearUserInfo()
    mainStore.activeIndex = '/'
    router.push({ name: 'login' })
  }
}
// const selectHandler = (index: string) => {
//   activeMenu.value = index
//   router.push(index)
// }
</script>

<style lang="scss" scoped>
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}

.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
}

.layout-container-demo .el-menu {
  border-right: none;
}

.layout-container-demo .el-main {
  padding: 0;
}

.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}

:deep(.el-scrollbar__view) {
  width: 100%;
  height: 100%;
}
</style>
